<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻详情页</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 2em;
            margin: 0;
        }

        .back-btn {
            background: rgba(255,255,255,0.2);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            padding: 10px 20px;
            border-radius: 5px;
            text-decoration: none;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: rgba(255,255,255,0.3);
            color: white;
            text-decoration: none;
        }

        .search-section {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            margin: 20px auto;
            max-width: 600px;
        }

        .search-form {
            display: flex;
            gap: 10px;
        }

        .search-input {
            flex: 1;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .search-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .search-btn {
            padding: 15px 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
        }

        .search-btn:hover {
            transform: translateY(-2px);
        }

        .content-section {
            background: white;
            margin: 20px auto;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .news-detail {
            padding: 40px;
        }

        .news-title {
            font-size: 2.2em;
            font-weight: bold;
            color: #333;
            margin-bottom: 20px;
            line-height: 1.4;
        }

        .news-meta {
            display: flex;
            gap: 30px;
            color: #666;
            font-size: 1em;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #f0f0f0;
        }

        .news-meta span {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .news-meta span::before {
            content: '';
            width: 4px;
            height: 4px;
            background: #667eea;
            border-radius: 50%;
        }

        .news-content {
            font-size: 1.1em;
            line-height: 1.8;
            color: #444;
            white-space: pre-wrap;
        }

        .search-results {
            padding: 20px;
            display: none;
        }

        .search-results.show {
            display: block;
        }

        .results-title {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            font-size: 1.5em;
            text-align: center;
        }

        .news-item {
            border-bottom: 1px solid #e0e0e0;
            padding: 20px 0;
            transition: background-color 0.3s;
        }

        .news-item:hover {
            background-color: #f8f9fa;
        }

        .news-item:last-child {
            border-bottom: none;
        }

        .news-item-title {
            font-size: 1.3em;
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
            cursor: pointer;
            transition: color 0.3s;
        }

        .news-item-title:hover {
            color: #667eea;
        }

        .news-item-meta {
            display: flex;
            gap: 20px;
            color: #666;
            font-size: 0.9em;
            margin-bottom: 10px;
        }

        .news-item-summary {
            color: #666;
            line-height: 1.6;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 1.1em;
        }

        .error {
            background: #ffebee;
            color: #c62828;
            padding: 20px;
            border-radius: 5px;
            margin: 20px;
            text-align: center;
        }

        .no-data {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 1.1em;
        }

        .highlight {
            background-color: #ffeb3b;
            padding: 2px 4px;
            border-radius: 2px;
        }

        .breadcrumb {
            background: white;
            padding: 15px 20px;
            margin: 20px auto;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .breadcrumb a {
            color: #667eea;
            text-decoration: none;
        }

        .breadcrumb a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="container">
            <div class="header-content">
                <h1>新闻详情</h1>
                <a href="index.html" class="back-btn">返回首页</a>
            </div>
        </div>
    </div>

    <div class="container">
        <!-- 面包屑导航 -->
        <div class="breadcrumb">
            <a href="index.html">首页</a> > 新闻详情
        </div>

        <!-- 搜索区域 -->
        <div class="search-section">
            <form class="search-form" id="searchForm">
                <input type="text" class="search-input" id="searchInput" placeholder="搜索其他新闻...">
                <button type="submit" class="search-btn">搜索</button>
            </form>
        </div>

        <!-- 新闻详情内容 -->
        <div class="content-section" id="newsDetailSection">
            <div class="news-detail" id="newsDetail">
                <div class="loading">正在加载新闻详情...</div>
            </div>
        </div>

        <!-- 搜索结果区域 -->
        <div class="content-section search-results" id="searchResults">
            <div class="results-title">搜索结果</div>
            <div class="search-results" id="searchResultsList">
                <!-- 搜索结果将通过JavaScript动态生成 -->
            </div>
        </div>
    </div>

    <script>
        // 获取URL参数
        function getUrlParameter(name) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }

        // 页面加载完成后获取新闻详情
        document.addEventListener('DOMContentLoaded', function() {
            const newsId = getUrlParameter('id');
            if (newsId) {
                loadNewsDetail(newsId);
            } else {
                showError('缺少新闻ID参数');
            }
        });

        // 搜索表单提交事件
        document.getElementById('searchForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const keyword = document.getElementById('searchInput').value.trim();
            if (keyword) {
                searchNews(keyword);
            }
        });

        // 加载新闻详情
        async function loadNewsDetail(newsId) {
            try {
                showLoading();
                const response = await fetch('/news/getNewsById', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `newsId=${newsId}`
                });
                
                const result = await response.json();
                
                if (result.code === 200 && result.data) {
                    displayNewsDetail(result.data);
                } else {
                    showError('加载新闻详情失败: ' + (result.message || '新闻不存在'));
                }
            } catch (error) {
                showError('网络错误: ' + error.message);
            }
        }

        // 搜索新闻
        async function searchNews(keyword) {
            try {
                showLoading();
                const response = await fetch('/news/search', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `keyword=${encodeURIComponent(keyword)}`
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    displaySearchResults(result.data, keyword);
                } else {
                    showError('搜索失败: ' + result.message);
                }
            } catch (error) {
                showError('搜索出错: ' + error.message);
            }
        }

        // 显示新闻详情
        function displayNewsDetail(news) {
            const newsDetailElement = document.getElementById('newsDetail');
            
            const html = `
                <div class="news-title">${news.title || '无标题'}</div>
                <div class="news-meta">
                    <span>作者: ${news.author || '未知'}</span>
                    <span>分类: ${news.type || '未分类'}</span>
                    <span>发布时间: ${formatDate(news.publicshDate)}</span>
                </div>
                <div class="news-content">${news.content || '暂无内容'}</div>
            `;

            newsDetailElement.innerHTML = html;
            
            // 更新页面标题
            document.title = (news.title || '新闻详情') + ' - 新闻详情页';
        }

        // 显示搜索结果
        function displaySearchResults(newsList, keyword) {
            const searchResultsElement = document.getElementById('searchResults');
            const searchResultsListElement = document.getElementById('searchResultsList');
            
            if (!newsList || newsList.length === 0) {
                searchResultsListElement.innerHTML = `<div class="no-data">未找到包含"${keyword}"的新闻</div>`;
            } else {
                let html = '';
                newsList.forEach(news => {
                    html += `
                        <div class="news-item" onclick="viewNewsDetail(${news.id})">
                            <div class="news-item-title">${highlightKeyword(news.title || '无标题', keyword)}</div>
                            <div class="news-item-meta">
                                <span>作者: ${highlightKeyword(news.author || '未知', keyword)}</span>
                                <span>分类: ${highlightKeyword(news.type || '未分类', keyword)}</span>
                                <span>发布时间: ${formatDate(news.publicshDate)}</span>
                            </div>
                            <div class="news-item-summary">${highlightKeyword(news.content ? news.content.substring(0, 150) + '...' : '暂无内容', keyword)}</div>
                        </div>
                    `;
                });
                searchResultsListElement.innerHTML = html;
            }

            // 显示搜索结果区域
            searchResultsElement.classList.add('show');
            
            // 滚动到搜索结果
            searchResultsElement.scrollIntoView({ behavior: 'smooth' });
        }

        // 高亮关键词
        function highlightKeyword(text, keyword) {
            if (!keyword) return text;
            const regex = new RegExp(`(${keyword})`, 'gi');
            return text.replace(regex, '<span class="highlight">$1</span>');
        }

        // 查看新闻详情
        function viewNewsDetail(newsId) {
            window.location.href = `search_details.html?id=${newsId}`;
        }

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return '未知时间';
            const date = new Date(dateString);
            return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN');
        }

        // 显示加载状态
        function showLoading() {
            document.getElementById('newsDetail').innerHTML = '<div class="loading">正在加载...</div>';
        }

        // 显示错误信息
        function showError(message) {
            document.getElementById('newsDetail').innerHTML = `<div class="error">${message}</div>`;
        }
    </script>
</body>
</html>